import React, {PureComponent} from 'react';
import {
	Button,
	StyleSheet,
	View,
	AsyncStorage
} from 'react-native';

// KEY
let STORAGE_KEY = 'TEST';

/**
 * @FileName: AsyncStorageDemo
 * @Author: mazaiting
 * @Date: 2018/6/20
 * @Description:
 */
class AsyncStorageDemo extends PureComponent {
	render() {
		return (
			<View style={styles.container}>
				<Button
					title={'保存数据'}
					onPress={() => this.saveData()}
					style={{
						marginTop: 10
					}}
				/>
				<Button
					title={'删除数据'}
					onPress={() => this.removeData()}
					style={{
						marginTop: 10
					}}
				/>
				<Button
					title={'获取数据'}
					onPress={() => this.getData()}
					style={{
						marginTop: 10
					}}
				/>
			</View>
		)
	}
	
	/**
	 * 保存数据
	 */
	async saveData() {
		alert("saveData")
		await AsyncStorage
			.setItem(STORAGE_KEY, 'mazaiting')
			.then(value => {
				alert("存储成功：" + value)
			})
			.catch(reason => {
				alert(reason.message)
			})
	}
	
	/**
	 * 删除数据
	 */
	async removeData() {
		await AsyncStorage
			.removeItem(STORAGE_KEY)
			.then(value => {
				alert("移除成功：" + value)
			})
			.catch(reason => {
				alert(reason.message)
			})
	}
	
	/**
	 * 获取数据
	 */
	async getData() {
		await AsyncStorage
			.getItem(STORAGE_KEY)
			.then(value => {
				alert("存储的值" + value)
			})
			.catch(reason => {
				alert(reason.message)
			})
	}
}

/**
 * 样式属性
 */
const styles = StyleSheet.create({
	container: {
		flex: 1,
		backgroundColor: '#DDD'
	}
});

/**
 * 导出当前Module
 */
module.exports = AsyncStorageDemo;